package io操作;

import java.io.*;
import java.util.ArrayList;

public class io缓冲流 {
    public static void new_file_write(File file, String[] data) throws IOException {
        BufferedWriter wt = new BufferedWriter(new FileWriter(file));
        for (String s : data)
            wt.write(s + "\r\n");
        wt.close();
    }

    public static ArrayList<String> new_file_read(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        ArrayList<String> ans = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            ans.add(s);
        }
        br.close();
        return ans;
    }

    public static void new_file_dir_copy_dfs(File src, File dest) {
        try {
            if (src == null) return;
            File[] files = src.listFiles();
            if (files == null) return;
            for (File file : files) {
                if (file.isFile()) {
                    new_file_copy(file.getPath(), dest.getPath() + "/" + file.getName());
                } else if (file.isDirectory()) {
                    File f = new File(dest, file.getName() + "副本");
                    f.mkdir();
                    new_file_dir_copy_dfs(file, f);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void new_file_copy(String src, String dest) throws Exception {
        try (BufferedInputStream fin = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(dest, true))) {
            byte[] bytes = new byte[2048];
            int bytesRead;
            while ((bytesRead = fin.read(bytes)) != -1) {
                fout.write(bytes, 0, bytesRead);
                fout.flush();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static void 文件字节输入流() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("file.txt");
        byte[] bytes = new byte[(int) new File("file.txt").length()];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = fileInputStream.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }
        System.out.println(sb);
        fileInputStream.close();
    }

    public static void 文件字节输出流() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("file.txt", true);
        String[] data = {"aeiruaghp", "vpiughepir", "t8lr8y", "a8er7hp"};
        for (String s : data) {
            fileOutputStream.write(s.getBytes());
            fileOutputStream.write("\r\n".getBytes());
        }
        fileOutputStream.close();
    }

    public static void 字符缓冲输入流() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        StringBuilder sb = new StringBuilder();
        while (br.ready()) {
            sb.append(br.readLine()).append("\r\n");
        }
        System.out.println(sb);
        br.close();
    }

    public static void 字符缓冲输出流() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt", true));
        String[] data = {"巧克力", "香子兰", "朝武芳乃"};
        for (String datum : data) {
            bw.write(datum);
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

    public static void 字符输入转换流() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("测试转换流文件.txt");
        InputStreamReader gbk = new InputStreamReader(fileInputStream, "GBK");
        BufferedReader br = new BufferedReader(gbk);
        StringBuilder sb = new StringBuilder();
        while (br.ready()) {
            sb.append(br.readLine()).append("\r\n");
        }
        System.out.println(sb);
        br.close();
        gbk.close();
        fileInputStream.close();
    }

    public static void 字符输出转换流() throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream("测试转换流文件.txt", true);
        OutputStreamWriter gbk = new OutputStreamWriter(fileOutputStream, "GBK");
        BufferedWriter bw = new BufferedWriter(gbk);
        String[] data = {"巧克力", "香子兰", "朝武芳乃"};
        for (String datum : data) {
            bw.newLine();
            bw.write(datum);
        }
        bw.flush();
        bw.close();
        gbk.close();
        fileOutputStream.close();
    }

    public static void 打印流() throws IOException {
        try (PrintStream ps = new PrintStream(new FileOutputStream("打印流.txt", true));
             PrintWriter pw = new PrintWriter(new FileWriter("打印流.txt", true))) {
            ps.println("巧克力");
            ps.println("香子兰");
            ps.println("朝武芳乃");
            ps.close();
            pw.println("巧克力");
            pw.println("香子兰");
            pw.println("朝武芳乃");
            pw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) throws Exception {
//        文件字节输出流();
//        文件字节输入流();
//        字符缓冲输出流();
//        字符缓冲输入流();
//        字符输入转换流();
//        字符输出转换流();
        打印流();
//        File src = new File("D:\\deskbook\\留校");
//        File dest = new File("D:\\deskbook\\留校副本");
//        dest.mkdir();
//        new_file_dir_copy_dfs(src, dest);
//        String[]data={
//                "aeiruaghp",
//                "vpiughepir",
//                "阿伟死了",
//                "a8er7hp"
//        };
//        new_file_write(new File("D:\\deskbook\\缓冲字节流.txt"),data);
//        var ans = new_file_read(new File("D:\\deskbook\\缓冲字节流.txt"));
//        System.out.println(ans);

    }
}
